const path = require('path')
const HtmlWebpackPlugin = require('html-webpack-plugin')
const {HotModuleReplacementPlugin: HMR} = require('webpack')
const MiniCssExtractPlugin = require('mini-css-extract-plugin')

module.exports = {
  mode: 'development',
  entry: {
    main: './src/main.js',
    search: './src/search.js'
  },
  output: {
    // JS 打包一般使用 chunk hash 策略
    filename: '[name]-[hash:8].js',
    path: path.resolve(__dirname, 'dist')
  },
  module: {
    rules: [
      {
        test: /\.(scss|css)$/,
        use: [
          'style-loader',
          'css-loader',
          'sass-loader'
        ]
      },
      // 图片的文件指纹一般是使用 hash
      {
        test: /\.(png|svg|jpg|gif)$/,
        use: [
          {
            loader: 'file-loader',
            options: {
              name: 'img/[name]-[hash:8].[ext]'
            }
          }
        ]
      }
    ]
  },
  plugins: [
    // css 文件一般使用 contenthash
    new MiniCssExtractPlugin({
      filename: `[name]-[contenthash:8].css`
    }),
    new HtmlWebpackPlugin({
      title: '学习文件指纹',
      template: path.resolve(__dirname, 'index.html')
    }),
    new HMR()
  ],
  devServer: {
    contentBase: './dist',
    hot: true
  }
}
